﻿
/// <reference path="/Scripts/jquery-1.5.1.min.js">
///<reference path="/Scripts/Common/Common">

var Playlist = function (mHost,options) {   
    var self = this;
    this.mHost = mHost;
    this.current = 0;
    $("#jquery_jplayer_2").jPlayer(options);

    $(" .jp-previous").click(function () {
        self.playlistPrev();
        $(this).blur();
        return false;
    });

    $(" .jp-next").click(function () {
        self.playlistNext();
        $(this).blur();
        return false;
    });
    $(".div_PlayListItem div:nth-child(2)").live("click", function () {
        var _index = $(".div_PlayListItem").index($(this).parent());        
        self.playlistChange(_index);
    });
    $(".div_PlayListItem div:nth-child(4)").live("click", function () {
        var _index = $(".div_PlayListItem").index($(this).parent());
       
        var guid = $(this).parent().attr("data-val");        
        jsPlayListStorage.deleteSingleSong(guid);
        $(this).parent().parent().remove();
        if (_index == self.current) {
            self.playlistChange(_index);
        }
    });
    //$(".div_PlayListItem").hover(function () {
    //    $(this).children().eq(3).addClass("delete_icon"); $(this).children().eq(3).attr("title", "从此列表移出");
    //}, function () {
    //    $(this).children().eq(3).removeClass("delete_icon");
    //});
    $(".div_PlayListItem").live({
        mouseenter: function () {
            $(this).children().eq(3).addClass("delete_icon"); $(this).children().eq(3).attr("title", "从此列表移出");
        },
        mouseleave: function () {
            $(this).children().eq(3).removeClass("delete_icon");
        }
    });
};

Playlist.prototype = {               
    playlistInit: function (autoplay) {
       // alert("aa");
        if (autoplay) {           
            this.playlistChange(this.current);
        } else {
            this.playlistConfig(this.current);
            
        }
    },
    playlistConfig: function (index) {
        $(".div_PlayListItem").not($(".div_PlayListItem").eq(index)).children(".divPlay").removeClass("divPlaying");
        $(".div_PlayListItem").eq(index).children(".divPlay").addClass("divPlaying");
        var mediaUrl = this.mHost + $(".div_PlayListItem").eq(index).attr("data-location");        
        $("#jquery_jplayer_2").jPlayer("setMedia", { m4a: mediaUrl });
    },
    playlistChange: function (index) {
        this.current = index;
        this.playlistConfig(index);
        $("#jquery_jplayer_2").jPlayer("play");
        var m_id = $(".div_PlayListItem").eq(index).attr("data-id");      
        increasePlayCount(m_id);
    },
    playlistNext: function () {
        var index = (this.current + 1 < $(".div_PlayListItem").length) ? this.current + 1 : 0;
        this.playlistChange(index);
    },
    playlistPrev: function () {
        var index = (this.current - 1 >= 0) ? this.current - 1 : $(".div_PlayListItem").length - 1;
        this.playlistChange(index);
    }
};

function increasePlayCount(id) {
    var playCount = 0;
    $.ajaxSetup({ async: false });
    $.get("/Handler/Common.ashx", { ID: id, action: "IncreasePlayCont" }, function (r) {
        playCount = r;
    });
    return playCount;
}